#include "mainwindow.h"
#include "ui_mainwindow.h"

#define RTMPURL "rtmp://192.168.10.86:1935/live/"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
    , rHandle(new RtmpHandle)
    , receive(new RtmpReceive)
    , isPushed(false)
{
    ui->setupUi(this);
    initControls();
    initHandle();

    connect(conButton, SIGNAL(clicked()), this,SLOT(receiveData()));
    connect(startButton, SIGNAL(clicked()), this,SLOT(startConnect()));
}

int MainWindow::initHandle()
{
    rHandle->SetObserver(this);

    receive->SetObserver(this);
    return 0;
}

MainWindow::~MainWindow()
{
    receive->StopAll();
    rHandle->StopAll();
    delete receive;
    delete rHandle;
    delete ui;
}

void MainWindow::initControls()
{
    QWidget *mainWindowWidget=new QWidget(this);
    QVBoxLayout *vLayout = new QVBoxLayout(this);
    QHBoxLayout *hLayout = new QHBoxLayout(this);
    QHBoxLayout *hLayout1 = new QHBoxLayout(this);
    mLable = new QLabel(this);
    rLable = new QLabel(this);
    mTxt = new QTextEdit();
    mTxt->setFixedHeight(25);
    mTxt->setFixedWidth(80);
    rTxt = new QTextEdit();
    rTxt->setFixedHeight(25);
    rTxt->setFixedWidth(80);
    startButton = new QPushButton("连接");
    startButton->setFixedWidth(80);
    conButton = new QPushButton("接收");
    conButton->setFixedWidth(80);
    QLabel *mlbl = new QLabel(this);
    mlbl->setText("推送号码：");
    mlbl->setFixedWidth(60);
    QLabel *rlbl = new QLabel(this);
    rlbl->setText("对方号码：");
    rlbl->setFixedWidth(60);

    hLayout1->addWidget(mlbl);
    hLayout1->addWidget(mTxt);
    hLayout1->addWidget(rlbl);
    hLayout1->addWidget(rTxt);
    hLayout1->addSpacing(20);
    hLayout1->addWidget(startButton);
    hLayout1->addWidget(new QLabel());
    hLayout1->addWidget(conButton);

//    hLayout1->setStretchFactor(mlbl, 2);
//    hLayout1->setStretchFactor(mTxt, 2);
//    hLayout1->setStretchFactor(rlbl, 2);
//    hLayout1->setStretchFactor(rTxt, 2);
//    hLayout1->setStretchFactor(conButton, 2);

    hLayout->addWidget(mLable);
    hLayout->addWidget(rLable);
    hLayout->setStretchFactor(mLable, 5);
    hLayout->setStretchFactor(rLable, 5);


    vLayout->addLayout(hLayout,9);
    vLayout->addLayout(hLayout1,1);

    mainWindowWidget->setLayout(vLayout);
    setCentralWidget(mainWindowWidget);
}

void MainWindow::ShowImage(QImage image)
{
    //方法二，按比例缩放
   // QImage image= MatToQImage2(mat);
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = mLable->width();
    int height = mLable->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    //按比例缩放
    mLable->setPixmap(fitpixmap);
}

void MainWindow::startConnect()
{
    QString sNum = mTxt->toPlainText();
    if(sNum.isEmpty()){
        return;
    }
    QString url = RTMPURL;
    url.append(sNum);

    startButton->setEnabled(false);
    rHandle->SetRtmpUrl(url.toStdString());
    rHandle->start();
}

void MainWindow::receiveData()
{
    isPushed = !isPushed;
    QString rNum = rTxt->toPlainText();
    QString url = RTMPURL;
    url.append(rNum);
    receive->SetRtmpUrl(url.toStdString());
   // if(!isPushed)  {
        qDebug() <<"start接收 "<< endl;
        conButton->setText("停止接收");
        receive ->start();
//    }else
//    {
//        qDebug() <<"stop接收 "<< endl;
//        conButton->setText("接收");
//        receive ->StopAll();
//    }

}

void MainWindow::OnReadVideo(QImage image)
{
     ShowImage(image);
}

void MainWindow::OnReceiveVideo(QImage image)
{
    QPixmap pixmap = QPixmap::fromImage(image);
    int width = rLable->width();
    int height = rLable->height();
    QPixmap fitpixmap = pixmap.scaled(width, height, Qt::KeepAspectRatio, Qt::SmoothTransformation);
    //按比例缩放
    rLable->setPixmap(fitpixmap);

}
